package com.wanpinghui.supplier.addrbook;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.message.MessageP2PActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.common.dialog.BaseDialogManager;
import com.wanpinghui.wphlibrary.common.dialog.YunBaseDialog;
import com.wanpinghui.wphlibrary.model.addrbook.AddrbookService;
import com.wanpinghui.wphlibrary.model.addrbook.bean.MoblieBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/11.
 */
@ContentView(R.layout.layout_activity_stranger_info)
public class StrangerInfoActivity extends BaseActivity {


    private TitlePanelLayoutUser tplTitle;

    private ImageView ivHead;

    private TextView tvName;

    private TextView tvAddress;

    private ImageView ivAuthentication;

    private TextView tvCom;

    private ThrottleButton tvImChat;

    private ThrottleButton tvPhoneCall;

    private ThrottleButton tvAddFriends;

    private MoblieBean bean;
    private String phone;

    private long user_id;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        initView();
        String status_display = getIntent().getStringExtra("status_display");
        if (status_display != null && status_display.contains("等待")){
            Drawable  drawable = ContextCompat.getDrawable(this,R.mipmap.message_icon_chat_adjunct_p);
            drawable.setBounds(0, 0, 30, 30);
            tvAddFriends.setCompoundDrawables(drawable,null,null,null);
            tvAddFriends.setText("等待认证");
            tvAddFriends.setEnabled(false);
            tvAddFriends.setTextColor(ContextCompat.getColor(this,R.color.colorTextPrimaryHint));
        }

    }

    @Override
    public void bindEvents() {
        tplTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvImChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user_id != 0) {
                    MessageP2PActivity.start(StrangerInfoActivity.this, String.valueOf(user_id));
                }

            }
        });
        tvPhoneCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (user_id != 0) {
                    //     tvPhoneCall.setEnabled(false);
                    WPHSdk.getService(AddrbookService.class).call(String.valueOf(user_id), new YRequestCallback<Object>() {
                        @Override
                        public void onSuccess(Object var1) {
                            //tvPhoneCall.setEnabled(true);
                            View view = LayoutInflater.from(StrangerInfoActivity.this).inflate(R.layout.view_dialog_call, null);
                            final Button button = (Button) view.findViewById(R.id.btn_right);
                            final YunBaseDialog dialog = BaseDialogManager.getInstance().getBuilder(StrangerInfoActivity.this).setContentView(view).create();
                            button.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    //     showWarningText("暂无虚拟号码");
//                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
//                          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                             startActivity(intent);
                                }
                            });
                            dialog.show();
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            showWarningText(message);
                        }

                        @Override
                        public void onException(Throwable var1) {
                            var1.printStackTrace();
                        }
                    });


                }
            }
        });
        tvAddFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tvAddFriends.getText().toString().contains("等待")) {
                    WPHSdk.getService(AddrbookService.class).addFriends(bean.getFollowed_id(), new YRequestCallback<Object>() {
                        @Override
                        public void onSuccess(Object var1) {
                            Drawable drawable = ContextCompat.getDrawable(StrangerInfoActivity.this, R.mipmap.message_icon_chat_adjunct_p);
                            drawable.setBounds(0, 0, 30, 30);
                            tvAddFriends.setCompoundDrawables(drawable, null, null, null);
                            tvAddFriends.setText("等待认证");

                            tvAddFriends.setTextColor(ContextCompat.getColor(StrangerInfoActivity.this, R.color.colorTextPrimaryHint));
                            showWarningText("等待认证");
                        }

                        @Override
                        public void onFailed(int var1, String message) {
                            showWarningText(message);

                        }

                        @Override
                        public void onException(Throwable var1) {
                            var1.printStackTrace();

                        }
                    });
                }
            }
        });

    }

    @Override
    public void initData() {
        bean = (MoblieBean) getIntent().getSerializableExtra("bean");
        if (bean != null) {
            user_id = bean.getUser_id();
            WPHSdk.getService(AddrbookService.class).getUserInfo(bean.getUser_id(), new YRequestCallback<UserBean>() {
                @Override
                public void onSuccess(UserBean var1) {
                    if (var1 != null && var1.getUser() != null) {
                        UserInformation userInformation = var1.getUser();
                        tvName.setText(userInformation.getNickname());

                        phone = userInformation.getMobile();
                        if (TextUtil.isNotEmpty(userInformation.getCom())) {
                            if (TextUtil.isNotEmpty(userInformation.getPos())) {
                                tvCom.setText(userInformation.getCom() + " | " + userInformation.getPos());
                            } else {
                                tvCom.setText(userInformation.getCom() + " | 暂无");
                            }

                        } else {
                            tvCom.setText("暂无他的公司信息/职称");
                        }
                        if (userInformation.isPass()) {
                            ivAuthentication.setVisibility(View.VISIBLE);
                        } else {
                            ivAuthentication.setVisibility(View.GONE);
                        }
                        if (TextUtil.isNotEmpty(userInformation.getAddr())){
                            tvAddress.setText(userInformation.getAddr());
                        }else {
                            tvAddress.setText(userInformation.getPhone_area_name());
                        }

                        Glide.with(getApplicationContext()).load(userInformation.getAvatar()).placeholder(R.mipmap.address_list_bg_head).bitmapTransform(new CropCircleTransformation(StrangerInfoActivity.this)).into(ivHead);
                    }
                }

                @Override
                public void onFailed(int var1, String message) {
                    showWarningText("获取信息失败");

                }

                @Override
                public void onException(Throwable var1) {
                    showWarningText("获取信息失败");
                }
            });
        }


    }

    private void initView() {
        tplTitle = (TitlePanelLayoutUser) findViewById(R.id.tpl_title);
        ivHead = (ImageView) findViewById(R.id.iv_head);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvAddress = (TextView) findViewById(R.id.tv_address);

        ivAuthentication = (ImageView) findViewById(R.id.iv_authentication);
        tvCom = (TextView) findViewById(R.id.tv_com);

        tvImChat = (ThrottleButton) findViewById(R.id.tv_im_chat);
        tvPhoneCall = (ThrottleButton) findViewById(R.id.tv_phone_call);
        tvAddFriends = (ThrottleButton) findViewById(R.id.tv_add_friends);
    }
}
